#include "MainFrame/ToolBar/Tools.h"
#include <QToolButton>
#include <QEvent>
#include "MainFrame/CentralWidget/CentralManager.h"
#include "Tools/ToolState.h"
#include "IRIVIATool.h"

Tools::Tools(QWidget *parent) :
    ToolBarBase(parent),
    m_pToolButtonGroup(nullptr),
    m_pPencilButton(nullptr),
    m_pPaintBrushButton(nullptr),
    m_pAirBrushButton(nullptr),
    m_pInkButton(nullptr),
    m_pEraserButton(nullptr),
    m_pBlurorSharpenButton(nullptr),
    m_pDodgeorBurnButton(nullptr),
    m_pSmudgeButton(nullptr),
    m_pBucketFillButton(nullptr),
    m_pRectangleSelectButton(nullptr),
    m_pEllipseSelectButton(nullptr),
    m_pFreeSelectButton(nullptr),
    m_pMoveButton(nullptr),
    m_pRotateButton(nullptr),
    m_pScaleButton(nullptr),
    m_pFlipButton(nullptr),
    m_pPathsButton(nullptr),
    m_pColorPickerButton(nullptr),
    m_pZoomButton(nullptr)
{
    initToolButton();
    initTrans();
    connectSignalToSlots();
}

Tools::~Tools()
{

}

void Tools::initToolButton()
{
    m_pPencilButton = addToolButton("Pencil");
    m_pPencilButton->setChecked(true);
    m_pPaintBrushButton = addToolButton("PaintBrush");
    m_pAirBrushButton = addToolButton("AirBrush");
    m_pInkButton = addToolButton("Ink");

    this->addSeparator();

    m_pEraserButton = addToolButton("Eraser");
    m_pBlurorSharpenButton = addToolButton("Blur or Sharpen");
    m_pDodgeorBurnButton = addToolButton("Dodge or Burn");
    m_pSmudgeButton = addToolButton("Smudge");
    m_pBucketFillButton = addToolButton("Bucket Fill");

    this->addSeparator();

    m_pRectangleSelectButton = addToolButton("Rectangle Select");
    m_pEllipseSelectButton = addToolButton("Ellipse Select");
    m_pFreeSelectButton = addToolButton("Free Select");

    this->addSeparator();

    m_pMoveButton = addToolButton("Move");
    m_pRotateButton = addToolButton("Rotate");
    m_pScaleButton = addToolButton("Scale");
    m_pFlipButton = addToolButton("Flip");

    this->addSeparator();

    m_pPathsButton = addToolButton("Paths");
    m_pColorPickerButton = addToolButton("Color Picker");
    m_pZoomButton = addToolButton("Zoom");

    m_pToolButtonGroup = new QButtonGroup();
    m_pToolButtonGroup->addButton(m_pPencilButton           , 1);
    m_pToolButtonGroup->addButton(m_pPaintBrushButton       , 2);
    m_pToolButtonGroup->addButton(m_pAirBrushButton         , 3);
    m_pToolButtonGroup->addButton(m_pInkButton              , 4);
    m_pToolButtonGroup->addButton(m_pEraserButton           , 5);
    m_pToolButtonGroup->addButton(m_pBlurorSharpenButton    , 6);
    m_pToolButtonGroup->addButton(m_pDodgeorBurnButton      , 7);
    m_pToolButtonGroup->addButton(m_pSmudgeButton           , 8);
    m_pToolButtonGroup->addButton(m_pBucketFillButton       , 9);
    m_pToolButtonGroup->addButton(m_pRectangleSelectButton  , 10);
    m_pToolButtonGroup->addButton(m_pEllipseSelectButton    , 11);
    m_pToolButtonGroup->addButton(m_pFreeSelectButton       , 12);
    m_pToolButtonGroup->addButton(m_pMoveButton             , 13);
    m_pToolButtonGroup->addButton(m_pRotateButton           , 14);
    m_pToolButtonGroup->addButton(m_pScaleButton            , 15);
    m_pToolButtonGroup->addButton(m_pFlipButton             , 16);
    m_pToolButtonGroup->addButton(m_pPathsButton            , 17);
    m_pToolButtonGroup->addButton(m_pColorPickerButton      , 18);
    m_pToolButtonGroup->addButton(m_pZoomButton             , 19);
}

void Tools::connectSignalToSlots()
{
    connect(m_pToolButtonGroup, SIGNAL(buttonClicked(int)),
           this, SLOT(onToolButtonChanged(int)));
}

void Tools::changeEvent(QEvent *event)
{
    QWidget::changeEvent(event);
    switch (event->type())
    {
    case QEvent::LanguageChange:
        initTrans();
        break;
    default:
        break;
    }
}

void Tools::initTrans()
{
    //set tool tips
    m_pPencilButton->setToolTip(tr("Pencil"));
    m_pPaintBrushButton->setToolTip(tr("PaintBrush"));
    m_pAirBrushButton->setToolTip(tr("AirBrush"));
    m_pInkButton->setToolTip(tr("Ink"));

    m_pEraserButton->setToolTip(tr("Eraser"));
    m_pBlurorSharpenButton->setToolTip(tr("Blur or Sharpen"));
    m_pDodgeorBurnButton->setToolTip(tr("Dodge or Burn"));
    m_pSmudgeButton->setToolTip(tr("Smudge"));
    m_pBucketFillButton->setToolTip(tr("Bucket Fill"));

    m_pRectangleSelectButton->setToolTip(tr("Rectangle Select"));
    m_pEllipseSelectButton->setToolTip(tr("Ellipse Select"));
    m_pFreeSelectButton->setToolTip(tr("Free Select"));

    m_pMoveButton->setToolTip(tr("Move"));
    m_pRotateButton->setToolTip(tr("Rotate"));
    m_pScaleButton->setToolTip(tr("Scale"));
    m_pFlipButton->setToolTip(tr("Flip"));

    m_pPathsButton->setToolTip(tr("Paths"));
    m_pColorPickerButton->setToolTip(tr("Color Picker"));
    m_pZoomButton->setToolTip(tr("Zoom"));
}

void Tools::onToolButtonChanged(int index)
{
    CentralManager::setEnCurToolState(ToolState(index));
    IRIVIATool().setEnCurTool(ToolState(index));
}
